package com.ym.b2b.order.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.order.dao.EmSOrderDao;
import com.ym.b2b.order.service.EmSOrderService;
import com.ym.b2b.order.sqlbean.EmSOrder;
import com.ym.b2b.order.vo.OrderInfoVO;
import com.ym.b2b.payment.dao.EmSPaymentDAO;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/trade")
public class OrderManagerController extends BaseController {

    @Autowired
    private EmSOrderService orderService;

    @Autowired
    private EmSOrderDao     emSOrderDao;

    @Autowired
    private EmSPaymentDAO   emSPaymentDAO;

    private Logger          logger = Logger.getLogger(getClass());

    /**
     * @Title: managerSearchOrder
     * @Description: 后台 订单列表
     * @Param @param emSOrder
     * @Param @param page
     * @Param @param req
     * @Param @return
     * @Return ModelAndView
     * @Throws
     */
    @RequestMapping("managerSearchOrder.do")
    @ResponseBody
    public Result managerSearchOrder(EmSOrder emSOrder, Page<EmSOrder> page, HttpServletRequest req) {

        Result result = new Result();
        try {
            page = orderService.searchPageOrders(emSOrder, page);
            result.setResultPage(page);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "searchPageEmSOrderExcel.do")
    @ResponseBody
    public Result searchPageEmSOrderExcel(EmSOrder emSOrder, Page<EmSOrder> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            page = orderService.searchPageOrders(emSOrder, page);
            ret.setResultPage(page);
            ExcelUtil<EmSOrder> excelUtil = new ExcelUtil<EmSOrder>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("orderListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping(value = "searchPageOrderTradeFeeMoneyExcel.do")
    @ResponseBody
    public Result searchPageOrderTradeFeeMoneyExcel(EmSOrder emSOrder, Page<EmSOrder> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            page = orderService.searchPageOrders(emSOrder, page);
            ret.setResultPage(page);
            ExcelUtil<EmSOrder> excelUtil = new ExcelUtil<EmSOrder>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("orderTradeFeeMoneyListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("searchOrderDetail.do")
    @ResponseBody
    public Result searchOrderDetail(OrderInfoVO emSOrder) {
        Result result = new Result();
        try {
            result = orderService.searchOrderDetail(emSOrder);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateOrder.do")
    @ResponseBody
    public Result updateOrder(EmSOrder emSOrder, HttpServletRequest req) {
        Result result = new Result();
        try {
            // 更新订单表
            emSOrderDao.updateEmSOrder(emSOrder);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 确认发货
    @RequestMapping("updateOrderDelivery.do")
    @ResponseBody
    public Result updateOrderDelivery(EmSOrder emSOrder, HttpServletRequest req) {
        Result result = new Result();
        try {
            // 更新订单表
            emSOrderDao.updateEmSOrderDelivery(emSOrder);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
}
